package com.ericcson.innerportalapp;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class EmergencyArrayAdapter extends ArrayAdapter<Emergency> {

	private int resource;
	private Context context;
	public EmergencyArrayAdapter(Context context, int resource,
			ArrayList<Emergency> list) {
		super(context, resource, list);
		this.resource = resource;
		this.context = context;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Emergency emergency = getItem(position);
		String name = emergency.getName();
		final String number = emergency.getNum();
		
		LayoutInflater layoutInflator = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View listViewItem = layoutInflator.inflate(resource, null);
		
		TextView textView = (TextView) listViewItem.findViewById(R.id.em1);
		TextView textView2 = (TextView) listViewItem.findViewById(R.id.em2);
		
		listViewItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent callIntent = new Intent(Intent.ACTION_CALL);
		        callIntent.setData(Uri.parse("tel:"+number));
		        context.startActivity(callIntent);
				
			}
		});
		
		textView.setText(name);
		textView2.setText(number);
		return listViewItem;
	}
}
